package marcis.jarina.hibernate.entities;

import marcis.jarina.directory.repository.Dependency;
import marcis.jarina.directory.repository.JarVersion;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Entity(name = "Archive")
@Table(name = "Archive")
public final class  Archive implements Serializable {

    private static final long serialVersionUID = 7526472295622776147L;

    private String storeDate;

    @Column(unique = true)
    private String filePath;

    @OneToMany(mappedBy = "archive",
            cascade = CascadeType.ALL,
            targetEntity = JavaClass.class)
    private List<JavaClass> containedClasses = new ArrayList<JavaClass>();

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String groupId;

    private String artifactId;

    private String version;

    @Transient
    private JarVersion jarVersion;


    @ManyToMany(cascade = CascadeType.ALL, targetEntity = Archive.class)
    private List<Archive> dependentArchives;


    public List<Archive> getDependentArchives() {
        return dependentArchives;
    }

    public void setDependentArchives(List<Archive> dependentArchives) {
        this.dependentArchives = dependentArchives;
    }

    public void addDependentArchive(Archive archive) {
        if (dependentArchives == null) {
            dependentArchives = new ArrayList<Archive>();
        }
        this.dependentArchives.add(archive);
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "archive_file_path")
    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public List<JavaClass> getContainedClasses() {
        return containedClasses;
    }

    public void setContainedClasses(List<JavaClass> containedClasses) {
        this.containedClasses = containedClasses;
    }

    public void addContainedClass(JavaClass src) {
        this.containedClasses.add(src);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("Archive file: ");
        sb.append("group id=");
        sb.append(groupId);
        sb.append(",artifact id=");
        sb.append(artifactId);
        sb.append(",version=");
        sb.append(version);
        sb.append(",store date=");
        sb.append(storeDate);
        sb.append("|");
        return sb.toString();
    }

    public void setStoreDate(String storeDate) {
        this.storeDate = storeDate;
    }

    public String getStoreDate() {
        return storeDate;
    }


    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }


    public String getArtifactId() {
        return artifactId;
    }

    public void setArtifactId(String artifactId) {
        this.artifactId = artifactId;
    }


    public JarVersion getJarVersion() {
        if (jarVersion == null && version != null) {
            jarVersion = JarVersion.valueOf(version);
        }
        return jarVersion;
    }

    public void setJarVersion(JarVersion jarVersion) {
        this.jarVersion = jarVersion;
        this.version = jarVersion.getFullVersion();
    }


    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
        this.jarVersion = JarVersion.valueOf(version);
    }

    public static Archive convert(Dependency dependency) {
        if (dependency == null) {
            return null;
        }
        Archive archive = new Archive();
        archive.setArtifactId(dependency.getArtifactId());
        archive.setGroupId(dependency.getGroupId());
        archive.setVersion(dependency.getVersion());
        return archive;
    }
}
